home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / forms / frmwiz / frmwizrd.bas < prev    next >
Encoding:
BASIC Source File  |  1995-01-15  |  19.4 KB  |  458 lines

  1. Option Explicit
  2. Global erraction As Integer
  3. Global Inst%
  4. Global ret As Variant
  5. Global db As database
  6. Global NewRecordSource As Integer
  7. Global Quote As String
  8. Global EndingIt As Integer
  9. Global aiFldSize() As Long       ' Field sizes
  10. Global RequiredFieldsComplete As String     ' Indication that required fields are present
  11.  
  12. Global stemplate As String, sForm As String    ' names for template and form files
  13. Global sFormLine As String, msg As String
  14. Global indent As Integer           ' number spaces to indent line
  15. Global iNumLabelLines As Integer   ' number of lines in label control definition
  16. Global sLabelLines() As String     ' lines in label definition
  17. Global iNumTextLines As Integer    ' number of lines in Text control definition
  18. Global sTextLines() As String      ' lines in textbox definition
  19. Global dSvLabel1Top As Double      ' save area for label1 top
  20. Global dSvLabel1Left As Double     ' save area for label1 left
  21. Global dSvLabel2Top As Double      ' save area for label2 top
  22. Global dLabelInc As Double         ' amount to increment each label top by
  23. Global dSvText1Top As Double       ' save area for textbox1 top
  24. Global dSvText1Left As Double      ' save area for textbox1 left
  25. Global dSvText2Top As Double       ' save area for textbox2 top
  26. Global dTextInc As Double          ' amount to increment each textbox top by
  27.  
  28.  
  29.  
  30.  
  31. ' API declares for 3d common controls
  32. Declare Function GetModuleUsage Lib "KERNEL" (ByVal InstanceID%) As Integer
  33. Declare Function GetModuleHandle Lib "Kernel" (ByVal ModuleName As String) As Integer
  34. Declare Function Ctl3dAutoSubclass Lib "Ctl3D.DLL" (ByVal hInst As Integer) As Integer
  35. Declare Function Ctl3dRegister Lib "Ctl3D.DLL" (ByVal hInst As Integer) As Integer
  36. Declare Function Ctl3dUnregister Lib "Ctl3D.DLL" (ByVal hInst As Integer) As Integer
  37.  
  38. Declare Function GetPrivateProfileInt Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Integer, ByVal lpFileName As String) As Integer
  39. Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
  40. Declare Function WritePrivateProfileString Lib "Kernel" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lplFileName As String) As Integer
  41.  
  42. 'Note: IniFile should be in Windows Directory
  43.  
  44. 'Example calling Code to create or update ini -------------------------------------------------------------------------------------
  45.     'IniFileName$ = "MyINI.INI"        'name of ini file
  46.     'AppName$ = "MyApp"             'Name of application or section heading
  47.     'KeyName$ = "MyNumber"          'Keyword or variable name
  48.     'NewVal$="MyNewValue"           'if Numeric value convert it to string
  49.     'SaveIni AppName$, IniFileName$, KeyName$, NewVal$
  50.  
  51. ' Example Calling Code to Read Numeric Variable ------------------------------------------------------------------------------
  52.     'IniFileName$ = "MyINI.INI"        'name of ini file
  53.     'AppName$ = "MyApp"             'Name of application or section heading
  54.     'KeyName$ = "MyNumber"          'Keyword or variable name
  55.     'nDefault = 0                   'Default numeric value (for numeric variables)
  56.     'Numeric%=TRUE                  'Tell it we are looking for numeric value
  57.     'ReadIni AppName$, KeyName$, nDefault, DefaultStr$, ReturnStr$, Numeric%, IniFileName$
  58.  
  59. ' Example Calling Code to Read String Variable ------------------------------------------------------------------------------
  60.     'IniFileName$ = "MyINI.INI"        'name of ini file
  61.     'AppName$ = "MyApp"             'Name of application or section heading
  62.     'KeyName$ = "MyString"          'Keyword or variable name
  63.     'DefaultStr$ = "DefaultString"  'Default string        (for String variables)
  64.     'Dim RetStr As String * 255     'Create an empty string to be filled
  65.     'nSize% = 255                   'uncertain - possibly length of fill string
  66.     'Numeric%=FALSE                 'Tell it we are looking for a string
  67.     'ReadIni AppName$, KeyName$, nDefault, DefaultStr$, ReturnStr$, Numeric%, IniFileName$
  68.  
  69. Sub Cancel3D ()
  70.     ' end the 3D effects
  71.     Inst% = GetModuleHandle(App.EXEName)  ' Get program's ModuleHandle.
  72.     ret = Ctl3dUnregister(Inst%)          ' Unregister the program.
  73.  
  74. End Sub
  75.  
  76. Sub EndItNow ()
  77.     On Error Resume Next
  78.     EndingIt = True
  79.     db.Close        ' Close the database
  80.     MainForm.Show MODELESS
  81.     Unload DataForm     ' Unload all possible forms
  82.     
  83. End Sub
  84.  
  85. Sub GotEndOfForm ()
  86.     Dim fld As Integer
  87.     Dim sFldName As String      ' Field name
  88.     Dim sFldCaption As String   ' Field Caption for label
  89.     Dim lnFldSize As Long       ' Field size
  90.     Dim lnFldWidth As Long      ' Field control width
  91.     Dim sCtrlLabel As String    ' Label for controls
  92.     Dim lFldSameLine As Integer ' Field go on same line?
  93.     Dim dFldTop As Double       ' Top of field control
  94.     Dim dFldLeft As Double      ' Left of field control
  95.     Dim dLblTop As Double       ' Top of label control
  96.     Dim dLblLeft As Double      ' Left of label control
  97.     Dim i As Integer
  98.     '   got the end of form line
  99.     On Error GoTo EndOfFormerr
  100.  
  101.  
  102.     If dSvText1Top = 0 Or dTextInc = 0 Then
  103.         Beep
  104.         Screen.MousePointer = DEFAULT
  105.         MsgBox "The data field control was either not specified or specified incorrectly in the template!", 0 + 48 + 0 + 0, "Form Save Error"
  106.         DataForm.TxtFrmName.SetFocus
  107.         Exit Sub
  108.     End If
  109.  
  110.     dFldTop = dSvText1Top - dLabelInc       ' Get top of first field control
  111.     dLblTop = dSvLabel1Top - dTextInc       ' and label control
  112.     ' output the label and field controls from the data saved earlier
  113.     For fld = 1 To DataForm.GrdFields.Rows - 1      ' Do it for each field in grid
  114.         DataForm.GrdFields.Row = fld
  115.         DataForm.GrdFields.Col = 0
  116.         sFldName = DataForm.GrdFields.Text
  117.         'Create Control Label from field name less special chars and spaces
  118.         sCtrlLabel = ""
  119.         For i = 1 To Len(sFldName)
  120.             If Mid$(sFldName, i, 1) > "/" And Mid$(sFldName, i, 1) < "{" Then
  121.                 sCtrlLabel = sCtrlLabel & Mid$(sFldName, i, 1)
  122.             End If
  123.         Next i
  124.         DataForm.GrdFields.Col = 1
  125.         sFldCaption = DataForm.GrdFields.Text
  126.         DataForm.GrdFields.Col = 2
  127.         If DataForm.GrdFields.Text = "Yes" Then
  128.             lFldSameLine = True
  129.         Else
  130.             lFldSameLine = False
  131.         End If
  132.         DataForm.GrdFields.Col = 3
  133.         lnFldSize = Val(DataForm.GrdFields.Text)
  134.         lnFldWidth = DataForm.TextWidth(String$(lnFldSize, "x"))
  135.         If lnFldWidth > Screen.Width - dSvText1Left - 300 Then
  136.             lnFldWidth = Screen.Width - dSvText1Left - 300
  137.         End If
  138.         indent = 3
  139.         ' Output the label for the current field
  140.         For i = 0 To iNumLabelLines
  141.             Select Case True
  142.             Case InStr(1, sLabelLines(i), "Begin ") <> 0        ' Begin line
  143.                 Print #2, Spc(indent); sLabelLines(i) & "Lbl" & sCtrlLabel
  144.                 indent = indent + 3
  145.             Case InStr(1, sLabelLines(i), "Caption ") <> 0      ' Caption = line
  146.                 Print #2, Spc(indent); Left$(sLabelLines(i), InStr(1, sLabelLines(i), "=") + 2) & Quote & sFldCaption & Quote
  147.             Case InStr(1, sLabelLines(i), "Top ") <> 0          ' Top = line
  148.                 If Not lFldSameLine Then
  149.                     dLblTop = dLblTop + dLabelInc       ' Increment top for next line
  150.                 End If
  151.                 Print #2, Spc(indent); Left$(sLabelLines(i), InStr(1, sLabelLines(i), "=") + 2) & Str$(dLblTop)
  152.             Case InStr(1, sLabelLines(i), "Alignment") <> 0         ' Alignment = line
  153.                 If lFldSameLine Then            ' If field on same line
  154.                     Print #2, Spc(indent); Left$(sLabelLines(i), InStr(1, sLabelLines(i), "=") + 2);
  155.                     Print #2, "  0    ' Left Justify"
  156.                 Else
  157.                     Print #2, Spc(indent); sLabelLines(i)       ' Use defined alignment
  158.                 End If
  159.             Case InStr(1, sLabelLines(i), "Left ") <> 0         ' Left = line
  160.                 Print #2, Spc(indent); Left$(sLabelLines(i), InStr(1, sLabelLines(i), "=") + 2);
  161.                 If Not lFldSameLine Then
  162.                     dLblLeft = dSvLabel1Left   ' Reset left if not on same line
  163.                 Else
  164.                     dLblLeft = dFldLeft
  165.                 End If
  166.                 Print #2, Str$(dLblLeft)
  167.                 dLblLeft = dLblLeft + DataForm.TextWidth(sFldCaption) + DataForm.TextWidth("   ")     ' Set left at next avail pos
  168.             Case InStr(1, sLabelLines(i), "End") <> 0        ' End line
  169.                 indent = indent - 3
  170.                 Print #2, Spc(indent); sLabelLines(i)
  171.             Case Else                                           ' All other lines
  172.                 Print #2, Spc(indent); sLabelLines(i)
  173.             End Select
  174.         Next i
  175.  
  176.         ' Output the field control for the current field
  177.         indent = 3
  178.         For i = 0 To iNumTextLines
  179.             Select Case True
  180.             Case InStr(1, sTextLines(i), "Begin ") <> 0        ' Begin line
  181.                 Print #2, Spc(indent); sTextLines(i) & "Txt" & sCtrlLabel
  182.                 indent = indent + 3
  183.             Case InStr(1, sTextLines(i), "Top ") <> 0          ' Top = line
  184.                 If Not lFldSameLine Then
  185.                     dFldTop = dFldTop + dTextInc       ' Increment top for next line
  186.                 End If
  187.                 Print #2, Spc(indent); Left$(sTextLines(i), InStr(1, sTextLines(i), "=") + 2) & Str$(dFldTop)
  188.             Case InStr(1, sTextLines(i), "Left ") <> 0         ' Left = line
  189.                 Print #2, Spc(indent); Left$(sTextLines(i), InStr(1, sTextLines(i), "=") + 2);
  190.                 If Not lFldSameLine Then
  191.                     dFldLeft = dSvText1Left   ' Reset left if not on same line
  192.                 Else
  193.                     dFldLeft = dLblLeft
  194.                 End If
  195.                 Print #2, Str$(dFldLeft)
  196.                 dFldLeft = dFldLeft + lnFldWidth + DataForm.TextWidth("   ")    ' Set left at next avail pos
  197.             Case InStr(1, sTextLines(i), "DataSource ") <> 0          ' DataSource = line
  198.                 Print #2, Spc(indent); Left$(sTextLines(i), InStr(1, sTextLines(i), "=") + 2) & Quote & DataForm.TxtName.Text & Quote
  199.             Case InStr(1, sTextLines(i), "DataField ") <> 0          ' DataField = line
  200.                 Print #2, Spc(indent); Left$(sTextLines(i), InStr(1, sTextLines(i), "=") + 2) & Quote & sFldName & Quote
  201.             Case InStr(1, sTextLines(i), "MaxLength ") <> 0          ' MaxLength = line
  202.                 Print #2, Spc(indent); Left$(sTextLines(i), InStr(1, sTextLines(i), "=") + 2) & Str$(lnFldSize)
  203.             Case InStr(1, sTextLines(i), "Width ") <> 0          ' Width = line
  204.                 Print #2, Spc(indent); Left$(sTextLines(i), InStr(1, sTextLines(i), "=") + 2) & Str$(lnFldWidth)
  205.             Case InStr(1, sTextLines(i), "End") <> 0        ' End line
  206.                 indent = indent - 3
  207.                 Print #2, Spc(indent); sTextLines(i)
  208.             Case Else                                           ' All other lines
  209.                 Print #2, Spc(indent); sTextLines(i)
  210.             End Select
  211.         Next i
  212.  
  213.     Next fld
  214.  
  215.  
  216.     ' output the end of form definition line
  217.     Print #2, sFormLine    ' Output the end line
  218.  
  219.     Exit Sub
  220.  
  221. EndOfFormerr:
  222.     erraction = RB_ErrorHandler("GenForm", "GotEndOfForm")
  223.     Select Case erraction
  224.     Case 1
  225.         Resume 0      ' Retry option selected
  226.     Case 2
  227.         Resume Next   ' Ignore option selected
  228.     End Select
  229.  
  230. End Sub
  231.  
  232. Sub ReadIni (AppName$, KeyName$, nDefault, DefaultStr$, ReturnStr$, Numeric%, IniFileName$)
  233.     Dim nSize%, lenRetString%
  234.     
  235. ' Read data from Private Profile (.ini) File
  236.  
  237.     If Numeric% Then    'we are looking for integer input
  238.         Numeric% = GetPrivateProfileInt(AppName$, KeyName$, nDefault, IniFileName$)
  239.     Else
  240.         Dim RetStr As String * 255 'Create an empty string to be filled
  241.         nSize% = 255               'uncertain - possibly length of fill string
  242.         lenRetString% = GetPrivateProfileString(AppName$, KeyName$, DefaultStr$, RetStr$, nSize%, IniFileName$)
  243.         ReturnStr$ = Left$(RetStr$, lenRetString%)
  244.         
  245.     End If
  246.  
  247.  
  248. End Sub
  249.  
  250. Sub SaveControl ()
  251.     ' output the data for a control other than fields and data control
  252.     On Error GoTo svcontrolerr
  253.             
  254.     Print #2, Spc(indent); sFormLine    ' Output the begin line
  255.     indent = indent + 3                 ' indent rest 1 position
  256.     Do While InStr(1, sFormLine, "End", 1) = 0
  257.         Input #1, sFormLine             ' Get the next line
  258.         If InStr(1, sFormLine, "End", 1) = 0 Then   ' if not end of control definition
  259.             Print #2, Spc(indent); sFormLine    ' Output each line of control definition
  260.         End If
  261.     Loop
  262.     indent = indent - 3                 ' Return indent back
  263.     Print #2, Spc(indent); sFormLine    ' Output the end line
  264.     
  265.     Exit Sub
  266.  
  267. svcontrolerr:
  268.     erraction = RB_ErrorHandler("GenForm", "SaveControl")
  269.     Select Case erraction
  270.     Case 1
  271.         Resume 0      ' Retry option selected
  272.     Case 2
  273.         Resume Next   ' Ignore option selected
  274.     End Select
  275.  
  276. End Sub
  277.  
  278. Sub SaveDataCtrl ()
  279.     ' Output the data for the data control
  280.     On Error GoTo svDataerr
  281.     ' Output beginning line with specified name
  282.     Print #2, Spc(indent); "Begin Data " & DataForm.TxtName.Text
  283.     indent = indent + 3     ' Indent data definition lines
  284.     Input #1, sFormLine     ' Get the next line of input
  285.     
  286.     Do While InStr(1, sFormLine, "End", 1) = 0
  287.         Select Case True
  288.         Case InStr(1, sFormLine, "Caption ") <> 0   ' Caption = line
  289.             Print #2, Spc(indent); "Caption  = "; Quote & DataForm.TxtCaption.Text & Quote
  290.         Case InStr(1, sFormLine, "DatabaseName ") <> 0   ' Database name = line
  291.             Print #2, Spc(indent); "DatabaseName  = "; Quote & DataForm.TxtDBName.Text & Quote
  292.         Case InStr(1, sFormLine, "RecordSource ") <> 0   ' RecordSource = line
  293.             Print #2, Spc(indent); "RecordSource  = " & Quote & DataForm.LstRecSrce.Text & Quote
  294.         Case Else
  295.             Print #2, Spc(indent); sFormLine        ' Output any unrecognized lines as is
  296.         End Select
  297.  
  298.         Input #1, sFormLine
  299.     Loop
  300.     indent = indent - 3     ' Reset indentation
  301.     Print #2, Spc(indent); sFormLine        ' Output the end line
  302.  
  303.     Exit Sub
  304.  
  305. svDataerr:
  306.     erraction = RB_ErrorHandler("GenForm", "SaveDataCtrl")
  307.     Select Case erraction
  308.     Case 1
  309.         Resume 0      ' Retry option selected
  310.     Case 2
  311.         Resume Next   ' Ignore option selected
  312.     End Select
  313.  
  314. End Sub
  315.  
  316. Sub SaveField1 ()
  317.     Dim i As Integer
  318.     ' Save the data for the first field textbox for later use
  319.     On Error GoTo svField1err
  320.     iNumLabelLines = 0
  321.     i = -1
  322.     sFormLine = Left$(sFormLine, Len(sFormLine) - 4)    ' Drop Fld1 from begin line
  323.     Do
  324.         i = i + 1       ' increment number lines in control definition
  325.         ReDim Preserve sTextLines(i) As String
  326.         sTextLines(i) = sFormLine      ' Save the line
  327.         If InStr(1, sFormLine, "Top ") <> 0 Then        ' Is this Top = line
  328.             dSvText1Top = Val(Mid$(sFormLine, InStr(4, sFormLine, "=") + 1))   ' Save value of top
  329.             dTextInc = dSvText2Top - dSvText1Top        ' Calc diff between top of fields
  330.         End If
  331.         If InStr(1, sFormLine, "Left ") <> 0 Then        ' Is this Left = line
  332.             dSvText1Left = Val(Mid$(sFormLine, InStr(4, sFormLine, "=") + 1))   ' Save value of Left
  333.         End If
  334.         Input #1, sFormLine
  335.     Loop While InStr(1, sFormLine, "End", 1) = 0
  336.     i = i + 1       ' increment number lines in control definition
  337.     ReDim Preserve sTextLines(i) As String
  338.     sTextLines(i) = sFormLine      ' Save the End line
  339.     iNumTextLines = i
  340.  
  341.  
  342.     Exit Sub
  343.  
  344. svField1err:
  345.     erraction = RB_ErrorHandler("GenForm", "SaveField1")
  346.     Select Case erraction
  347.     Case 1
  348.         Resume 0      ' Retry option selected
  349.     Case 2
  350.         Resume Next   ' Ignore option selected
  351.     End Select
  352.  
  353. End Sub
  354.  
  355. Sub SaveField2 ()
  356.     ' Save the data for the first label for later use
  357.     On Error GoTo svField2err
  358.     Do While InStr(1, sFormLine, "End", 1) = 0
  359.         If InStr(1, sFormLine, "Top ") <> 0 Then        ' Is this top = line
  360.             dSvText2Top = Val(Mid$(sFormLine, InStr(4, sFormLine, "=") + 1))   ' Save value of top
  361.             dTextInc = dSvText2Top - dSvText1Top        ' Calc diff between top of fields
  362.         End If
  363.         Input #1, sFormLine
  364.     Loop
  365.  
  366.     Exit Sub
  367.  
  368. svField2err:
  369.     erraction = RB_ErrorHandler("GenForm", "SaveField2")
  370.     Select Case erraction
  371.     Case 1
  372.         Resume 0      ' Retry option selected
  373.     Case 2
  374.         Resume Next   ' Ignore option selected
  375.     End Select
  376.  
  377. End Sub
  378.  
  379. Sub SaveIni (AppName$, IniFileName$, KeyName$, NewVal$)
  380.     Dim ResultCode%
  381.  
  382.     ' Update INI file
  383.         
  384.     ResultCode% = WritePrivateProfileString(AppName$, KeyName$, NewVal$, IniFileName$)
  385.     If ResultCode% = 0 Then
  386.         Beep
  387.         MsgBox "Error updating INI file!", 16, "ERROR!"
  388.     End If
  389.  
  390.  
  391.     
  392. End Sub
  393.  
  394. Sub SaveLabel1 ()
  395.     Dim i As Integer        ' number of lines in control definition
  396.     
  397.     ' Save the data for the first label for later use
  398.     On Error GoTo svlabel1err
  399.     i = -1
  400.     sFormLine = Left$(sFormLine, Len(sFormLine) - 4)    ' Drop Lbl1 from begin line
  401.     Do
  402.         i = i + 1       ' increment number lines in control definition
  403.         ReDim Preserve sLabelLines(i) As String
  404.         sLabelLines(i) = sFormLine      ' Save the line
  405.         If InStr(1, sFormLine, "Top ") <> 0 Then        ' Is this top = line
  406.             dSvLabel1Top = Val(Mid$(sFormLine, InStr(4, sFormLine, "=") + 1))   ' Save value of top
  407.             dLabelInc = dSvLabel2Top - dSvLabel1Top     ' Calculate difference between top of labels
  408.         End If
  409.         If InStr(1, sFormLine, "Left ") <> 0 Then        ' Is this Left = line
  410.             dSvLabel1Left = Val(Mid$(sFormLine, InStr(4, sFormLine, "=") + 1))   ' Save value of Left
  411.         End If
  412.         Input #1, sFormLine
  413.     Loop While InStr(1, sFormLine, "End", 1) = 0
  414.     i = i + 1       ' increment number lines in control definition
  415.     ReDim Preserve sLabelLines(i) As String
  416.     sLabelLines(i) = sFormLine      ' Save the End line
  417.     iNumLabelLines = i
  418.  
  419.     Exit Sub
  420.  
  421. svlabel1err:
  422.     erraction = RB_ErrorHandler("GenForm", "SaveLabel1")
  423.     Select Case erraction
  424.     Case 1
  425.         Resume 0      ' Retry option selected
  426.     Case 2
  427.         Resume Next   ' Ignore option selected
  428.     End Select
  429.  
  430. End Sub
  431.  
  432. Sub SaveLabel2 ()
  433.     ' Use the top of the second label to determine the increment
  434.     ' for successive field labels
  435.     On Error GoTo svlabel2err
  436.     Do While InStr(1, sFormLine, "End", 1) = 0
  437.         If InStr(1, sFormLine, "Top ") <> 0 Then        ' Is this top = line
  438.             dSvLabel2Top = Val(Mid$(sFormLine, InStr(4, sFormLine, "=") + 1))   ' Save value of top
  439.             dLabelInc = dSvLabel2Top - dSvLabel1Top     ' Calculate difference between top of labels
  440.         End If
  441.         Input #1, sFormLine
  442.     Loop
  443.  
  444.     Exit Sub
  445.  
  446. svlabel2err:
  447.     erraction = RB_ErrorHandler("GenForm", "SaveLabel2")
  448.     Select Case erraction
  449.     Case 1
  450.         Resume 0      ' Retry option selected
  451.     Case 2
  452.         Resume Next   ' Ignore option selected
  453.     End Select
  454.  
  455.  
  456. End Sub
  457.  
  458.